import Colors from "./Colors";
import { api_member_info_index, api_member_info_company, api_member_info_follow, api_get_service_url, api_member_info_cancelFollow, api_get_setting, api_account_getgraphics } from "./HttpServices";
import { postFetch } from "./HttpExtension";
import { show } from "./Toast";
import { contactBrowser } from "./Gobrowser";

class CommonUtil {
    static addZero(count) {
        if (count < 10) {
            return '0' + count;
        }
        return count;
    }

    //检查字符串是否数字
    static checkRate(nubmer) {
        //判断正整数/[1−9]+[0−9]∗]∗/
        var re = /^[0-9]+.?[0-9]*/;//
        if (!re.test(nubmer)) {
            return false;
        }
        return true;
    }

    //获取不能缩放的Html
    static getHtml(data) {
        return '<!DOCTYPE html><html><body>' +
            '<head>' +
            '<meta name="viewport" content="width=device-width, initial-scale=1.0,maximum-scale=1.0, minimum-scale=1.0, user-scalable=no">' +
            '</head>' +
            `${data}` +
            '<script>window.onload=function(){window.location.hash = 1;document.title = document.body.clientHeight;}</script>' +
            '</body></html>';
    }//            'window.location.hash = "#" + height;document["title"] = height;' +

    static getHeightHtml(data) {
        return `<!DOCTYPE html><html><body>
        <head>
        <meta name="viewport" content="width=device-width, initial-scale=1.0,maximum-scale=1.0, minimum-scale=1.0, user-scalable=no">
        </head>
        ${data}
        <script>
            var height = null;
            function changeHeight() {
              if (document.body.scrollHeight != height) {
                height = document.body.scrollHeight;
                if (window.ReactNativeWebView.postMessage) {
                    window.ReactNativeWebView.postMessage(JSON.stringify({
                    type: 'setHeight',
                    height: height,
                  }))
                }
              }
            }
            setTimeout(changeHeight, 300)
        </script>
      </body>
    </html>`
    }

    static BaseScript =
        `
    var height = null;
    function changeHeight() {
      if (document.body.scrollHeight != height) {
        height = document.body.scrollHeight;
        if (window.ReactNativeWebView.postMessage) {
            window.ReactNativeWebView.postMessage(JSON.stringify({
            type: 'setHeight',
            height: height,
          }))
        }
      }
    }
    setTimeout(changeHeight, 300)
    `

    //获取设置
    static getSetting() {
        let params = {

        }
        postFetch(api_get_setting, params, {
            success: (response) => {
                global.setting = response.result;
            },
            fail: (response) => {
                show(response.result.message);
            }
        })
    }

    //获取图形验证码
    static getGraphics(callback) {
        let params = {

        }
        postFetch(api_account_getgraphics, params, {
            success: (response) => {
                callback.success ? callback.success(response) : null
            },
            fail: (response) => {
                show(response.result.message);
                callback.fail ? callback.fail(response) : null
            }
        })
    }

    //获取用户信息
    static getUserInfo() {
        let params = {

        }
        postFetch(api_member_info_index, params, {
            success: (response) => {
                global.userInfo = response.result;
            },
            fail: (response) => {
                show(response.result.message);
            }
        })
    }

    //获取公司信息
    static getCompanyInfo() {
        let params = {

        }
        postFetch(api_member_info_company, params, {
            success: (response) => {
                global.companyInfo = response.result;
            },
            fail: (response) => {
                // show(response.result.message);
            }
        })
    }

    //关注
    static follow(merchid, callback) {
        let params = {
            merchid: merchid
        }
        postFetch(api_member_info_follow, params, {
            success: (response) => {
                callback.success ? callback.success(response) : null
            },
            fail: (response) => {
                show(response.result.message);
                callback.fail ? callback.fail(response) : null
            }
        })
    }

    //取消关注
    static cancelfollow(merchid, callback) {
        let params = {
            merchid: merchid
        }
        postFetch(api_member_info_cancelFollow, params, {
            success: (response) => {
                callback.success ? callback.success(response) : null
            },
            fail: (response) => {
                show(response.result.message);
                callback.fail ? callback.fail(response) : null
            }
        })
    }

    //获取客服链接
    static getKFLink() {
        let params = {

        }
        postFetch(api_get_service_url, params, {
            success: (response) => {
                contactBrowser(response.result.url)
            },
            fail: (response) => {
                show(response.result.message);
            }
        })
    }

    static getDescArrow(current, type1, type2) {
        if (current == type1) {
            return require('../res/fws_xllll.png');//金色向下
        } else if (current == type2) {
            return require('../res/fws_xl.png');//金色向上
        } else {
            return require('../res/ic_arrow_2.png');//黑色向下
        }
    }


    //获取接单类型
    static getOrderTakingType(type) {
        switch (type) {
            case '1':
                return '来料加工';
            case '2':
                return '包工包料';
            default:
                return '未知'
        }
    }

    //获取加工方式
    static getWorkingType(type) {
        switch (type) {
            case '1':
                return '设计加工';
            case '2':
                return '来图加工';
            case '3':
                return '来样加工';
            default:
                return '未知'
        }
    }

    //获取支付方式
    static getPayType(type) {
        switch (type) {
            case '1':
                return '平台支付';
            case '2':
                return '线下对接';
            default:
                return '未知'
        }
    }

    //获取配送方式
    static getOtherType(type) {
        switch (type) {
            case '1':
                return '含运费';
            case '2':
                return '不含运费';
            case '3':
                return '管家配送';
            default:
                return '未知'
        }
    }

    static getPayStatusText(type) {
        if (type == '-1') {
            return '已取消'
        } else if (type == '0') {
            return '等待买家付款'
        } else if (type == '1') {
            return '待发货'
        } else if (type == '2') {
            return '卖家已发货'
        } else if (type == '3') {
            return '订单完成'
        }
    }

    static getCommentStatusText(type) {
        if (type == '1') {
            return '差评'
        } else if (type == '2') {
            return '中评'
        } else if (type == '3') {
            return '好评'
        }
    }

    static checkText(text) {
        if (text) {
            return text
        } else {
            return ''
        }
    }
}

export default CommonUtil;